package org.example.seata.common.security.config.resource;

import lombok.AllArgsConstructor;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * 本地token服务
 *
 * @author hzq
 * @date 2021/12/8 14:41
 */
@AllArgsConstructor
public class CustomLocalTokenServices implements ResourceServerTokenServices {

    private final TokenStore tokenStore;

    @Override
    public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException {
        return tokenStore.readAuthentication(accessToken);
    }

    @Override
    public OAuth2AccessToken readAccessToken(String accessToken) {
        return tokenStore.readAccessToken(accessToken);
    }

    public static CustomLocalTokenServices build(TokenStore tokenStore) {
        return new CustomLocalTokenServices(tokenStore);
    }
}
